package com.liang.paul.officelite.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.dd.processbutton.iml.ActionProcessButton;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.beams.OfficeUser;

import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends FastActivity {

    ActionProcessButton btn_login;
    EditText name, pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        btn_login = f(R.id.btn_file_download);
        name = f(R.id.editText_login_name);
        pwd = f(R.id.editText_login_pwd);
        btn_login.setMode(ActionProcessButton.Mode.ENDLESS);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_login.setProgress(1);
                final OfficeUser user = new OfficeUser();
                user.setUsername(name.getText().toString());
                user.setPassword(pwd.getText().toString());
                user.login(getApplicationContext(), new SaveListener() {
                    @Override
                    public void onSuccess() {
                        if (user.getEmailVerified()) {
                            btn_login.setProgress(0);
                            finish();
                            startActivity(new Intent(LoginActivity.this, TeamActivity.class));
                        } else {
                            btn_login.setProgress(0);
                            Toast.makeText(getApplicationContext(), "邮箱未验证", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        btn_login.setProgress(0);
                        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    public void goRegist(View view){
        startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
    }

    public void goErro(View view){
        startActivity(new Intent(LoginActivity.this, ErroActivity.class));
    }
}
